package com.zt.dev.physicalequipment.controller;

import com.zt.dev.common.ServerResponse;
import com.zt.dev.physicalequipment.pojo.*;
import com.zt.dev.physicalequipment.service.PhysicalEquipmentService;
import com.zt.dev.physicalequipment.utils.BeanUtil;
import com.zt.dev.physicalequipment.utils.ExcelUtil;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("physical")
public class PhysicalEquipmentController {

    @Autowired
    private PhysicalEquipmentService equipmentService;

    /**
     * 获取全部装备名称或仿真模型
     * @return
     */
    @PostMapping("getAllEquipment")
    public ServerResponse getAllEquipment() {
        List<PhysicalEquipment> equipmentList = equipmentService.getAllEquipment();
        return ServerResponse.createBySuccess(equipmentList);
    }

    /**
     * 实装信息注册
     * @param message
     * @return
     */
    @PostMapping("register")
    public ServerResponse register(@RequestBody MessageDTO message) {
        int result = equipmentService.register(message);
        if (result == 1) {
            return ServerResponse.createBySuccess(message);
        } else {
            return ServerResponse.createByErrorMessage("实装信息注册失败");
        }
    }

    /**
     * 新增实装
     * @param equipment
     * @return
     */
    @PostMapping("insertEquipment")
    public ServerResponse insertEquipment(EquipmentDTO equipment) {
        int result = equipmentService.insertEquipment(equipment);
        if (result == 1) {
            return ServerResponse.createBySuccessMessage("新增实装成功");
        } else {
            return ServerResponse.createByErrorMessage("新增实装失败");
        }
    }

    /**
    * 实装信息编辑
    * @param equipment
    * @return
    */
    @PostMapping("updateEquipmentMessage")
    public ServerResponse updateEquipmentMessage(PhysicalEquipmentMessage equipment) {
        int result = equipmentService.updateEquipmentMessage(equipment);
        if (result == 1) {
            return ServerResponse.createBySuccessMessage("编辑实装成功");
        } else {
            return ServerResponse.createByErrorMessage("编辑实装失败");
        }
    }

    /**
     * 通过实装查询性能
     * @param equipmentId
     * @return
     */
    @PostMapping("getTypesByEquipment")
    public ServerResponse getTypesByEquipment(Integer equipmentId) {
        List<PhysicalType> types = equipmentService.getTypesByEquipment(equipmentId);
        return ServerResponse.createBySuccess(types);
    }

    /**
     * 分页查询全部装备
     * @param currentPage
     * @param pageSize
     * @return
     */
    @PostMapping("getAllEquipmentMessage")
    public ServerResponse getAllEquipmentMessage(@RequestParam(value = "currentPage", defaultValue = "1", required = false) int currentPage, int pageSize) {
        PageBean<PhysicalEquipmentMessage> messages = equipmentService.getAllEquipmentMessage(currentPage,pageSize);
        return ServerResponse.createBySuccess(messages);
    }

    /**
     * 查询全部装备
     * @return
     */
    @PostMapping("getAllEquipmentMessageWithoutPageBean")
    public ServerResponse getAllEquipmentMessageWithoutPageBean() {
        List<PhysicalEquipmentMessage> messages = equipmentService.getAllEquipmentMessageWithoutPageBean();
        return ServerResponse.createBySuccess(messages);
    }

    /**
     * 复制实装信息
     * @param ids
     * @return
     */
    @PostMapping("copy")
    public ServerResponse copy(Integer[] ids) {
        List<PhysicalEquipmentMessage> messageList = equipmentService.copy(ids);
        return ServerResponse.createBySuccess(messageList);
    }

    /**
     * 删除实装信息
     * @param ids
     * @return
     */
    @PostMapping("deleteMessages")
    public ServerResponse deleteMessages(Integer[] ids) {
        int result = equipmentService.deleteMessages(ids);
        return ServerResponse.createBySuccess("删除了" + result + "条数据");
    }

    /**
     * 查询具体实装信息
     * @param id
     * @return
     */
    @PostMapping("getMessageDetail")
    public ServerResponse getMessageDetail(Integer id) {
        MessageDTO message = equipmentService.getMessageDetail(id);
        return ServerResponse.createBySuccess(message);
    }

    /**
     * 导出
     * @param response
     */
    @RequestMapping("export")
    public void export(HttpServletResponse response, Integer[] ids){
        try {
            String fileName = "实装信息表" + ".xls";   
            OutputStream os = response.getOutputStream();    
            fileName = new String(fileName.getBytes(),"ISO8859-1");
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition","attachment;filename="+fileName);
            response.addHeader("Pargam","no-cache");
            response.addHeader("Cache-Control","no-cache");
            HSSFWorkbook wb = null;
            if (ids == null || ids.length == 0) {
                wb = equipmentService.export();
            } else {
                wb = equipmentService.exportByIds(ids);
            }
            if (wb != null) {
                wb.write(os);
            }
            os.flush();
            os.close();  
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导出规范
     * @param response
     */
    @RequestMapping("exportHeader")
    public void exportHeader(HttpServletResponse response){
        try {
            String fileName = "实装信息表导入规范" + ".xls";
            fileName = new String(fileName.getBytes(),"ISO8859-1");
            OutputStream os = response.getOutputStream();
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition","attachment;filename="+fileName);
            response.addHeader("Pargam","no-cache");
            response.addHeader("Cache-Control","no-cache");
            HSSFWorkbook wb = equipmentService.exportHeader();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }  

    @RequestMapping("import")
    public ServerResponse importPhysical(MultipartFile file){
        boolean result = equipmentService.importPhysical(file);
        if (result) {
            return ServerResponse.createBySuccess("导入成功");
        } else {
            return ServerResponse.createByErrorMessage("导入失败");
        }
    }

}
